#!/usr/bin/python3.4
# -*- coding=utf-8 -*-
import sys
from io import StringIO
import logging
logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
import re
from scapy.all import *
import sys

def dns(dns_name):
    f_handler = StringIO()

	__console__=sys.stdout

	sys.stdout = f_handler

	dns_result = sr(IP(dst="114.114.114.114")/UDP()/DNS(rd=1,qd=DNSQR(qname=dns_name)), verbose=False)

	dns_result[0][0][1].show()

	sys.stdout=__console__

	dns_raw = f_handler.getvalue()

	result_type = re.findall('.*\s+(type)\s*=\s*(.*).*', dns_raw)
	result_data = re.findall('.*\s+(rdata)\s*=\s*(.*).*', dns_raw)

	result_type_data = zip(result_type, result_data)

	for x,y in result_type_data:
		print('%s %-5s %s %s' % (x[0], x[1], y[0], y[1]))
		
if __name__ == '__main__':
	dns(sys.argv[1])

##############################################################
#[root@Fedora python]# ./scapy_dns_1.py www.baidu.com
#type CNAME rdata 'www.a.shifen.com.'
#type A     rdata '61.135.169.121'
#type A     rdata '61.135.169.125'
